<template>
	<sw-form-frame v-model="visible" :title="formName" width="560" :type="formType" :loading="btnLoading" footer @submit="handleSubmitForm()">
		<el-form ref="form" size="medium" :model="formData" :rules="formRules" label-width="80px">
			<el-row :gutter="16">
				<el-col :span="24">
					<el-form-item label="流程名称" prop="name">
						<el-input v-model="formData.name"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="业务范围" prop="extFiled">
						<el-select v-model="formData.extFiled" placeholder="选择业务范围">
							<el-option v-for="(item, index) in dictEnums['FLOW_TYPE']" :key="index" :label="item.msg" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序" prop="orderBy">
						<el-input-number v-model="formData.orderBy" controls-position="right" :min="1" style="width: 100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="流程描述" prop="des">
						<el-input type="textarea" v-model="formData.des" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="输入流程描述"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</sw-form-frame>
</template>

<script>
import { mapGetters } from 'vuex';
import { addedFlow, updateFlow } from '@/api/approval/flowDefinition';
export default {
	props: {
		value: { type: Boolean, default: false },
		data: { type: Object, default: () => {} }
	},
	data() {
		return {
			visible: this.value,
			formData: {},
			formRules: {
				name: [{ required: true, message: '请输入流程名称', trigger: 'blur' }],
				extFiled: [{ required: true, message: '请选择业务范围', trigger: 'change' }]
			},
			formName: '流程',
			btnLoading: false
		};
	},
	computed: {
		...mapGetters(['dictEnums']),
		addOrEdit() {
			return Object.keys(this.data).length === 0;
		},
		formType() {
			return this.addOrEdit ? 'add' : 'edit';
		},
		formTypeText() {
			return this.formType === 'edit' ? '修改' : '新增';
		},
		callFunction() {
			return eval(this.addOrEdit ? addedFlow : updateFlow);
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['form'].resetFields();
				if (!!this.addOrEdit) {
					this.formData = {};
				} else {
					this.formData = { ...this.data };
				}
			});
		}
	},
	methods: {
		//提交表单
		handleSubmitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				this.submitLoading = true;
				this.submitFormRequest();
			});
		},

		//添加修改请求
		async submitFormRequest() {
			let params = { ...this.formData };
			let result = await this.callFunction(params).then((res) => res.data);
			this.submitLoading = false;
			if (result.code === '0') {
				this.visible = false;
				this.$emit('finish');
				this.$message.success(`${this.dialogTitle}成功`);
			}
		}
	}
};
</script>
